package com.jstyle.blesdk.Util;


import android.util.Log;

import com.jstyle.blesdk.constant.DeviceConst;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.blesdk.model.DeviceBean;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

/**
 * Created by Administrator on 2018/1/17.
 */

public class ResolveUtil {
  public static   int hrvLength =15;
  public static   int activityLength =24;

    /**
     * crc校验
     *
     * @param value
     */
    public static void crcValue(byte[] value) {
        byte crc = 0;
        for (int i = 0; i < value.length - 1; i++) {
            crc += value[i];
        }
        value[value.length - 1] = (byte) (crc & 0xff);
    }

    public static byte getTimeValue(int value) {
        String data = value + "";
        Integer m = Integer.parseInt(data, 16);
        return (byte) m.intValue();
    }

    /**
     * 设备时间
     *
     * @param value
     * @return
     */
    public static String getDeviceTime(byte[] value) {
        String date = bcd2String(value[1]) + "-"
                + bcd2String(value[2]) + "-" + bcd2String(value[3]) + " "
                + bcd2String(value[4]) + ":" + bcd2String(value[5]) + ":" + bcd2String(value[6]);

        return date;
    }
    public static String getGpsTime(byte[] value) {

        String gpsDate = bcd2String(value[9]) + "."
                + bcd2String(value[10]) + "." + bcd2String(value[11]) ;

        return gpsDate;
    }

    /**
     * 用户信息
     *
     * @param value
     * @return
     */
    public static String[] getUserInfo(byte[] value) {
        String[] userInfo = new String[6];
        for (int i = 0; i < 5; i++) {
            userInfo[i] = String.valueOf(getValue(value[i + 1], 0));
        }
        String deviceId = "";
        for (int i = 6; i < 12; i++) {
            if (value[i] == 0)
                continue;
            deviceId += (char) getValue(value[i], 0);
        }
        userInfo[5] = deviceId;
        return userInfo;
    }


    /**
     * 设备信息
     *
     * @param value
     * @return
     */
    public static String[] getDeviceInfo(byte[] value) {
        String[] userInfo = new String[8];
        for (int i = 0; i < 6; i++) {
            userInfo[i] = String.valueOf(getValue(value[i + 1], 0));
        }
        userInfo[6] = String.valueOf(getValue(value[9], 0));
        userInfo[7] = String.valueOf(getValue(value[11], 0));
        return userInfo;
    }

    /**
     * 实时计步信息
     *
     * @param value
     * @return
     */
    public static String[] getActivityData(byte[] value) {

        String[] activityData = new String[6];
        int step = 0;
        float cal = 0;
        float distance = 0;
        int time = 0;
        int heart = 0;
        int exerciseTime = 0;
        for (int i = 1; i < 5; i++) {
            step += getValue(value[i], i - 1);
        }
        for (int i = 5; i < 9; i++) {
            cal += getValue(value[i], i - 5);
        }
        for (int i = 9; i < 13; i++) {
            distance += getValue(value[i], i - 9);
        }
        for (int i = 13; i < 17; i++) {
            time += getValue(value[i], i - 13);
        }
        for (int i = 17; i < 21; i++) {
            exerciseTime += getValue(value[i], i - 17);
        }
        heart = getValue(value[21], 0);
        NumberFormat numberFormat = getNumberFormat(1);
        BigDecimal bigDecimal=new BigDecimal(String.valueOf(cal/100));
        BigDecimal bigDecimaCal= bigDecimal.setScale(1, RoundingMode.HALF_DOWN);
        activityData[0] = String.valueOf(step);
        activityData[1] = bigDecimaCal.floatValue()+"";
        numberFormat.setMinimumFractionDigits(2);
        String distanceString= numberFormat.format(distance / 100);
        activityData[2] =distanceString.replace(",",".");
        activityData[3] = String.valueOf(time/60);
        activityData[4] = String.valueOf(heart);
        activityData[5] = String.valueOf(exerciseTime);
        return activityData;
    }

    /**
     * 步数目标
     *
     * @param value
     * @return
     */
    public static String getGoal(byte[] value) {
        int goal = 0;
        for (int i = 0; i < 4; i++) {
            goal += getValue(value[i + 1], i);
        }
        return String.valueOf(goal);
    }

    /**
     * 设备电量
     *
     * @param value
     * @return
     */
    public static String getDeviceBattery(byte[] value) {
        int battery = getValue(value[1], 0);
        return String.valueOf(battery);
    }

    /**
     * 设备mac地址
     *
     * @param value
     * @return
     */
    public static String getDeviceAddress(byte[] value) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < 7; i++) {
            sb.append(String.format("%02X", value[i])).append(":");
        }
        String address = sb.toString();
        return address.substring(0, address.lastIndexOf(":"));
    }

    /**
     * 设备版本号
     *
     * @param value
     * @return
     */
    public static String getDeviceVersion(byte[] value) {
        //    String version = "";
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i < 5; i++) {
            stringBuffer.append(String.format("%X", value[i])).append(i == 4 ? "" : ".");
        }
        return stringBuffer.toString();
    }

    /**
     * 设备名字
     *
     * @param value
     * @return
     */
    public static String getDeviceName(byte[] value) {
        String name = "";
        for (int i = 1; i < 15; i++) {
            if (value[i] == 0)
                continue;
            name += (char) getValue(value[i], 0);
        }
        return name;
    }

    public static String getByteString(byte b) {
        byte[] array = new byte[8];
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i <= 7; i++) {
            array[i] = (byte) (b & 1);
            b = (byte) (b >> 1);
            stringBuffer.append(String.valueOf(array[i])).append(i == 7 ? "" : "-");
        }
        return stringBuffer.toString();
    }

    public static String getByteArray(byte b) {
        byte[] array = new byte[8];
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i <= 7; i++) {
            array[i] = (byte) (b & 1);
            b = (byte) (b >> 1);
            stringBuffer.append(String.valueOf(array[i]));
        }
        return stringBuffer.toString();
    }

    /**
     * 自动测量心率时间段
     *
     * @param value
     * @return
     */
    public static String[] getAutoHeart(byte[] value) {
        String[] autoHeart = new String[7];
        int enable = getValue(value[1], 0);
        String startHour = bcd2String(value[2]);
        String startMin = bcd2String(value[3]);
        String stopHour = bcd2String(value[4]);
        String stopMin = bcd2String(value[5]);
        String week = getByteString(value[6]);
        int time = getValue(value[7], 0) + getValue(value[8], 1);
        autoHeart[0] = String.valueOf(enable);
        autoHeart[1] = startHour;
        autoHeart[2] = startMin;
        autoHeart[3] = stopHour;
        autoHeart[4] = stopMin;
        autoHeart[5] = week;
        autoHeart[6] = String.valueOf(time);
        return autoHeart;
    }

    /**
     * 运动提醒
     *
     * @param value
     * @return
     */
    public static String[] getActivityAlarm(byte[] value) {
        String[] activityAlarm = new String[7];
        String startHour = bcd2String(value[1]);
        String startMin = bcd2String(value[2]);
        String stopHour = bcd2String(value[3]);
        String stopMin = bcd2String(value[4]);
        String week = getByteString(value[5]);
        int time = getValue(value[6], 0);
        int step = getValue(value[7], 0);
        activityAlarm[0] = startHour;
        activityAlarm[1] = startMin;
        activityAlarm[2] = stopHour;
        activityAlarm[3] = stopMin;
        activityAlarm[4] = week;
        activityAlarm[5] = String.valueOf(time);
        activityAlarm[6] = String.valueOf(step);
        return activityAlarm;
    }

    /**
     * 总运动数据
     *
     * @param value
     * @return
     */
    public static DeviceBean getTotalStepData(byte[] value) {
        int count = getStepCount(value);
        int length = value.length;
        int size = length / count;
        DeviceBean deviceBean = new DeviceBean();
        List<Map<String, String>> list = new ArrayList<>();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumFractionDigits(2);
        numberFormat.setGroupingUsed(false);
        if(value[value.length-1]==  (byte) 0xff)  deviceBean.setFinish(true);
        for (int i = 0; i < size; i++) {

            Map<String, String> hashMap = new HashMap<>();
            String date = bcd2String(value[2 + i * count]) + "." +
                    bcd2String(value[3 + i * count]) + "." + bcd2String(value[4 + i * count]);
            int step = 0;
            int time = 0;
            float cal = 0;
            float distance = 0;

            for (int j = 0; j < 4; j++) {
                step += getValue(value[5 + j + i * count], j);
            }
            for (int j = 0; j < 4; j++) {
                time += getValue(value[9 + j + i * count], j);
            }
            for (int j = 0; j < 4; j++) {
                distance += getValue(value[13 + j + i * count], j);
            }
            for (int j = 0; j < 4; j++) {
                cal += getValue(value[17 + j + i * count], j);
            }
            int exerciseTime = 0;
            int goal = count==26?getValue(value[21 + i * count], 0):(getValue(value[21 + i * count], 0)+getValue(value[22 + i * count], 1));

            for (int j = 0; j < 4; j++) {
                exerciseTime += getValue(value[count-4 + j + i * count], j);
            }
            hashMap.put(DeviceKey.KHistoryDate, date);
            hashMap.put(DeviceKey.KHistorySteps, String.valueOf(step));
            hashMap.put(DeviceKey.KHistorySportTime, String.valueOf(time/60));
            hashMap.put(DeviceKey.KHistoryCalories, numberFormat.format(cal / 100));
            hashMap.put(DeviceKey.KHistoryDistance, numberFormat.format(distance / 100));
            hashMap.put(DeviceKey.KHistoryGoal, String.valueOf(goal));
            hashMap.put(DeviceKey.KHistoryExerciseTime, String.valueOf(exerciseTime));
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    private static int getStepCount(byte[]value){
        int goal=27;
        int length=value.length;
        if(length!=2){
            if(length%26==0){
                goal=26;
            }else if(length%27==0){
                goal=27;
            }else{
                if((length-2)%26==0){
                    goal=26;
                }else if((length-2)%27==0){
                    goal=27;
                }
            }
        }
        return goal;
    }
    /**
     * 详细运动数据
     *
     * @param value
     * @return
     */
    public static DeviceBean getDetailData(byte[] value) {
        int count = 25;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setGroupingUsed(false);
        if ( value[length-1] == (byte) 0xff) {
            deviceBean.setFinish(true);
        }
        for (int i = 0; i < size; i++) {
            Map<String, String> hashMap = new HashMap<>();
            int flag = 1 + (i + 1) * count;
            String date = bcd2String(value[3 + i * 25]) + "."
                    + bcd2String(value[4 + i * 25]) + "." + bcd2String(value[5 + i * 25]) + " "
                    + bcd2String(value[6 + i * 25]) + ":" + bcd2String(value[7 + i * 25]) + ":" + bcd2String(value[8 + i * 25]);
            int step = 0;
            float cal = 0;
            float distance = 0;
            StringBuffer stringBuffer = new StringBuffer();
            for (int j = 0; j < 2; j++) {
                step += getValue(value[9 + j + i * 25], j);
            }
            for (int j = 0; j < 2; j++) {
                cal += getValue(value[11 + j + i * 25], j);
            }
            for (int j = 0; j < 2; j++) {
                distance += getValue(value[13 + j + i * 25], j);
            }
            for (int j = 0; j < 10; j++) {
                stringBuffer.append(String.valueOf(getValue(value[15 + j + i * 25], 0))).append(j == 9 ? "" : " ");
            }
            hashMap.put(DeviceKey.KDetailStepDate, date);
            hashMap.put(DeviceKey.KDetailSteps, String.valueOf(step));
            hashMap.put(DeviceKey.KDetailCalories, numberFormat.format(cal / 100));
            hashMap.put(DeviceKey.KDetailDistance, numberFormat.format(distance / 100));
            hashMap.put(DeviceKey.KDetailMinterStep, stringBuffer.toString());
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    /**
     * 睡眠数据
     *
     * @param value
     * @return
     */
    public static DeviceBean getSleepData(byte[] value) {
        int count = 34;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        if(value[value.length-1]==  (byte) 0xff)  deviceBean.setFinish(true);
        for (int i = 0; i < size; i++) {
//            int flag = 1 + (i + 1) * count;
//            if (flag < length && value[flag] == (byte) 0xff) {
//                deviceBean.setFinish(true);
//            }
            Map<String, String> hashMap = new HashMap<>();
            String date = bcd2String(value[3 + i * 34]) + "."
                    + bcd2String(value[4 + i * 34]) + "." + bcd2String(value[5 + i * 34]) + " "
                    + bcd2String(value[6 + i * 34]) + ":" + bcd2String(value[7 + i * 34]) + ":" + bcd2String(value[8 + i * 34]);
            hashMap.put(DeviceKey.KSleepDate, date);
            int sleepLength= getValue(value[9 + i * 34], 0) ;
            StringBuffer stringBuffer = new StringBuffer();
            for (int j = 0; j < sleepLength; j++) {
                stringBuffer.append(String.valueOf(getValue(value[10 + j + i * 34], 0))).append(j == sleepLength ? "" : " ");
            }
            hashMap.put(DeviceKey.KSleepQuantity, stringBuffer.toString());
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    /**
     * 历史心率数据
     *
     * @param value
     * @return
     */
    public static DeviceBean getHeartData(byte[] value) {
        int count = 24;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        if(value[length-1]==(byte)0xff) deviceBean.setFinish(true);
        for (int i = 0; i < size; i++) {
            int flag = 1 + (i + 1) * count;

            Map<String, String> hashMap = new HashMap<>();
            String date = bcd2String(value[3 + i * count]) + "."
                    + bcd2String(value[4 + i * count]) + "." + bcd2String(value[5 + i * count]) + " "
                    + bcd2String(value[6 + i * count]) + ":" + bcd2String(value[7 + i * count]) + ":" + bcd2String(value[8 + i * count]);


            StringBuffer stringBuffer = new StringBuffer();
            for (int j = 0; j < 15; j++) {
                stringBuffer.append(String.valueOf(getValue(value[9 + j + i * count], 0))).append(j == 14 ? "" : " ");
            }
            hashMap.put(DeviceKey.KHeartDate, date);
            hashMap.put(DeviceKey.KHeartData, stringBuffer.toString());
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    /**
     * 单次历史心率数据
     *
     * @param value
     * @return
     */
    public static DeviceBean getOnceHeartData(byte[] value) {
        int count = 10;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        if ( value[length-1] == (byte) 0xff) {
            deviceBean.setFinish(true);
        }
        for (int i = 0; i < size; i++) {
            int flag = 1 + (i + 1) * count;
            Map<String, String> hashMap = new HashMap<>();
            String date = bcd2String(value[3 + i * 10]) + "."
                    + bcd2String(value[4 + i * 10]) + "." + bcd2String(value[5 + i * 10]) + " "
                    + bcd2String(value[6 + i * 10]) + ":" + bcd2String(value[7 + i * 10]) + ":" + bcd2String(value[8 + i * 10]);
            String heart = String.valueOf(getValue(value[9 + i * 10], 0));
            hashMap.put(DeviceKey.KOnceHeartDate, date);
            hashMap.put(DeviceKey.KOnceHeartValue, heart);
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    public static Map<String, String> getEcgPPGResult(byte[]value){
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put(DeviceKey.ECGPPGResult,getValue(value[1],0)+"");
        return hashMap;
    }
    /**
     * htv测试数据
     *
     * @param value
     * @return
     */
    public static DeviceBean getHrvTestData(byte[] value) {
        int count = hrvLength;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        if(value[value.length-1]==  (byte) 0xff)  deviceBean.setFinish(true);
        for (int i = 0; i < size; i++) {
//            int flag = 1 + (i + 1) * count;
////            if (flag < length && value[flag] == (byte) 0xff) {
////                deviceBean.setFinish(true);
////            }
            Map<String, String> hashMap = new HashMap<>();
            String date = bcd2String(value[3 + i * count]) + "."
                    + bcd2String(value[4 + i * count]) + "." + bcd2String(value[5 + i * count]) + " "
                    + bcd2String(value[6 + i * count]) + ":" + bcd2String(value[7 + i * count]) + ":" + bcd2String(value[8 + i * count]);
            String hrv = String.valueOf(getValue(value[9 + i * count], 0));
            String blood = String.valueOf(getValue(value[10 + i * count], 0));
            String heart = String.valueOf(getValue(value[11 + i * count], 0));
            String Tired = String.valueOf(getValue(value[12 + i * count], 0));
            String highBloodPressure = String.valueOf(getValue(value[13 + i * count], 0));
            String lowBloodPressure = String.valueOf(getValue(value[14 + i * count], 0));
            if(hrvLength ==17){
                String moodValue = String.valueOf(getValue(value[15 + i * count], 0));
                String breathRate = String.valueOf(getValue(value[16 + i * count], 0));
                hashMap.put(DeviceKey.KHrvMoodValue, moodValue);
                hashMap.put(DeviceKey.KHrvBreathRate, breathRate);
            }

            hashMap.put(DeviceKey.KHrvDate, date);
            hashMap.put(DeviceKey.KHrvValue, hrv);
            hashMap.put(DeviceKey.KHrvBloodValue, blood);
            hashMap.put(DeviceKey.KHrvHeartValue, heart);
            hashMap.put(DeviceKey.KHrvTired, Tired);
            hashMap.put(DeviceKey.KHrvBloodHighPressure, highBloodPressure);
            hashMap.put(DeviceKey.KHrvBloodLowPressure, lowBloodPressure);

            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    /**
     * 闹钟数据
     *
     * @param value
     * @return
     */
    public static DeviceBean getClockData(byte[] value) {
        int count = 41;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        if(value[length-1]==(byte)0xff)deviceBean.setFinish(true);
        for (int i = 0; i < size; i++) {

            Map<String, String> hashMap = new HashMap<>();
            String id = String.valueOf(getValue(value[4 + i * count], 0));
            String enable = String.valueOf(getValue(value[5 + i * count], 0));
            String type = String.valueOf(getValue(value[6 + i * count], 0));
            String hour = bcd2String(value[7 + i * count]);
            String min = bcd2String(value[8 + i * count]);
            String week = getByteString(value[9 + i * count]);
            int lengthS = getValue(value[10 + i * count], 0);
            String content = "";
            for (int J = 0; J < lengthS; J++) {
                if (value[11 + i * count] == 0)
                    continue;
                content += (char) getValue(value[11 + J + i * count], 0);
            }
            hashMap.put(DeviceKey.KAlarmId, id);
            hashMap.put(DeviceKey.KAlarmEnable, enable);
            hashMap.put(DeviceKey.KAlarmType, type);
            hashMap.put(DeviceKey.KAlarmHour, hour);
            hashMap.put(DeviceKey.KAlarmMinter, min);
            hashMap.put(DeviceKey.KAlarmWeekEnable, week);
            hashMap.put(DeviceKey.KAlarmContent, content);
            hashMap.put(DeviceKey.KAlarmLength, String.valueOf(lengthS));
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    private static final String TAG = "ResolveUtil";

    public static DeviceBean getHistoryGpsData(byte[] value) {
        int count = 59;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        if(value[value.length-1]== (byte) 0xff&&value[value.length-2]==(byte)0x5a)  deviceBean.setFinish(true);
        for (int i = 0; i < size; i++) {
            int flag = 1 + (i + 1) * count;

            Map<String, String> hashMap = new HashMap<>();
            int id=getValue(value[1+i*count],0)+getValue(value[2+i*count],1);
            String date = bcd2String(value[3 + i * count]) + "."
                    + bcd2String(value[4 + i * count]) + "." + bcd2String(value[5 + i * count]) + " "
                    + bcd2String(value[6 + i * count]) + ":" + bcd2String(value[7 + i * count]) + ":" + bcd2String(value[8 + i * count]);
            byte[] valueLatitude = new byte[4];
            byte[] valueLongitude = new byte[4];
            StringBuffer stringBufferLatitude = new StringBuffer();
            StringBuffer stringBufferLongitude = new StringBuffer();
            for (int k = 0; k < 6; k++) {
                for (int j = 0; j < 4; j++) {
                    valueLatitude[3 - j] = value[9 + j + i * count + k * 8];
                    valueLongitude[3 - j] = value[13 + j + i * count + k * 8];
                }
                String Latitude = String.valueOf(getFloat(valueLatitude, 0));
                String Longitude = String.valueOf(getFloat(valueLongitude, 0));
                stringBufferLatitude.append(Latitude).append(k == 5 ? "" : ",");
                stringBufferLongitude.append(Longitude).append(k == 5 ? "" : ",");
            }
            hashMap.put(DeviceKey.KLocationTime, date.length()==17?date:"19.01.01 00:00:00");
            hashMap.put(DeviceKey.KDataID,String.valueOf(id));
            hashMap.put(DeviceKey.KLocationLatitude, stringBufferLatitude.toString());
            hashMap.put(DeviceKey.KLocationLongitude, stringBufferLongitude.toString());
            hashMap.put(DeviceKey.KIsLocationStart, value[58 + i * count] == (byte) 0xff ? "1" : "0");
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    public static Map<String, String> getActivityGpsData(byte[] value) {


        Map<String, String> hashMap = new HashMap<>();
        String date = bcd2String(value[1]) + "-"
                + bcd2String(value[2]) + "-" + bcd2String(value[3]) + " "
                + bcd2String(value[4]) + ":" + bcd2String(value[5]) + ":" + bcd2String(value[6]);
        byte[] valueLatitude = new byte[4];
        byte[] valueLongitude = new byte[4];
        for (int j = 0; j < 4; j++) {
            valueLatitude[3 - j] = value[9+j ];
            valueLongitude[3 - j] = value[14+j];
        }
        String Latitude = String.valueOf(getFloat(valueLatitude, 0));
        String Longitude = String.valueOf(getFloat(valueLongitude, 0));
        int count=getValue(value[18],0);

        hashMap.put(DeviceKey.KActivityLocationTime, date);
        hashMap.put(DeviceKey.KActivityLocationLatitude, Latitude);
        hashMap.put(DeviceKey.KActivityLocationLongitude, Longitude);
        hashMap.put(DeviceKey.KActivityLocationCount, String.valueOf(count));

        return hashMap;
    }

    public static DeviceBean getExerciseData(byte[] value) {
        int count = 25;
        int length = value.length;
        int size = length / count;
        List<Map<String, String>> list = new ArrayList<>();
        DeviceBean deviceBean = new DeviceBean();
        if (size == 0) {
            deviceBean.setDataList(list);
            deviceBean.setFinish(true);
            return deviceBean;
        }
        NumberFormat numberFormatCal =getNumberFormat(1);
        NumberFormat numberFormat =getNumberFormat(2);
        for (int i = 0; i < size; i++) {
            int flag = 1 + (i + 1) * count;
            if (flag < length &&  i==size-1&&value[flag] == (byte) 0xff) {
                deviceBean.setFinish(true);
            }
            Map<String, String> hashMap = new HashMap<>();
            String date = bcd2String(value[3 + i * count]) + "."
                    + bcd2String(value[4 + i * count]) + "." + bcd2String(value[5 + i * count]) + " "
                    + bcd2String(value[6 + i * count]) + ":" + bcd2String(value[7 + i * count]) + ":" + bcd2String(value[8 + i * count]);
            String mode = String.valueOf(getValue(value[9 + i * count], 0));
            String heartRate = String.valueOf(getValue(value[10 + i * count], 0));
            int periodTime = getData(2, 11 + i * count, value);
            int steps = getData(2, 13 + i * count, value);
            int speedMin = getValue(value[15 + i * count],0);
            int speedS = getValue(value[16 + i * count],0);
            byte[] valueCal = new byte[4];
            for (int j = 0; j < 4; j++) {
                valueCal[3 - j] = value[17 +j+ i * count];
            }
            byte[] valueDistance = new byte[4];
            for (int j = 0; j < 4; j++) {
                valueDistance[3 - j] = value[21 +j+ i * count];
            }
            float cal = getFloat(valueCal,0);
            float distance = getFloat(valueDistance,0);
            hashMap.put(DeviceKey.KSportDate, date);
            hashMap.put(DeviceKey.KSportModel, mode);
            hashMap.put(DeviceKey.KSportHeart, heartRate);
            hashMap.put(DeviceKey.KSPortModelTime, String.valueOf(periodTime));
            hashMap.put(DeviceKey.KSportSteps, String.valueOf(steps));
            hashMap.put(DeviceKey.KSportSpeedSeconds, String.valueOf(speedMin + speedS * 256));
            hashMap.put(DeviceKey.KSportSpeed, String.format("%02d",speedMin)+"'"+String.format("%02d",speedS)+"\"");
            hashMap.put(DeviceKey.KSportDistance, numberFormat.format(distance));

            hashMap.put(DeviceKey.KSportCalories, numberFormatCal.format(cal));
            list.add(hashMap);
        }
        deviceBean.setDataList(list);
        return deviceBean;
    }

    public static Map<String, String> getActivityExerciseData(byte[] value) {
        Map<String, String> map = new HashMap<>();
        int heartRate = getValue(value[1], 0);
        int steps = 0;
        float kcal = 0;
        for (int i = 0; i < 4; i++) {
            steps += getValue(value[i + 2], i);
        }
        byte[] valueCal = new byte[4];
        for (int i = 0; i < 4; i++) {
            valueCal[3 - i] = value[i + 6];
            // kcal+=getFloat(value[i+6],i);
        }
        kcal = getFloat(valueCal, 0);
        NumberFormat numberFormat =getNumberFormat(2);
        map.put(DeviceKey.KExerciseHeart, String.valueOf(heartRate));
        map.put(DeviceKey.KExerciseStep, String.valueOf(steps));
        map.put(DeviceKey.KExerciseCal, numberFormat.format(kcal));
        return map;
    }
    public static Map<String, String> setTimeSuccessful(byte[] value) {//设置时间成功后返回手机一个数据包的最大长度
        Map<String, String> map = new HashMap<>();
        map.put(DeviceKey.KPhoneDataLength, String.valueOf(getValue(value[1],0)));
        return map;
    }
    public static Map<String, String> function(byte[] value) {
        Map<String, String> map = new HashMap<>();
        if(value[1]==2) map.put(DeviceKey.KFunction_Carmra, "");
        if(value[1]==4) map.put(DeviceKey.KFunction_FindPhone, "");
        if(value[1]==1&&value[2]==0) map.put(DeviceKey.KFunction_reject_tel, "2");
        if(value[1]==1&&value[2]==1) map.put(DeviceKey.KFunction_tel, "0");
        return map;
    }
    public static Map<String, String> updateClockSuccessful(byte[] value) {
        Map<String, String> map = new HashMap<>();
        map.put(DeviceKey.KClockLast, String.valueOf(getValue(value[value.length-1],0)));
        return map;
    }

    public static int getData(int length, int start, byte[] value) {
        int data = 0;
        for (int j = 0; j < length; j++) {
            data += getValue(value[j + start], j);
        }
        return data;
    }

    public static int getValue(byte b, int count) {
        return (int) ((b & 0xff) * Math.pow(256, count));
    }

    public static String bcd2String(byte bytes) {
        StringBuffer temp = new StringBuffer();
        temp.append((byte) ((bytes & 0xf0) >>> 4));
        temp.append((byte) (bytes & 0x0f));
        return temp.toString();
    }

    public static float getFloat(byte[] arr, int index) {//字节转浮点
        return Float.intBitsToFloat(getInt(arr, index));
    }

    public static int getInt(byte[] arr, int index) {
        return (0xff000000 & (arr[index + 0] << 24)) |
                (0x00ff0000 & (arr[index + 1] << 16)) |
                (0x0000ff00 & (arr[index + 2] << 8)) |
                (0x000000ff & arr[index + 3]);
    }
    public static NumberFormat getNumberFormat(int max){
        NumberFormat numberFormat=NumberFormat.getNumberInstance(Locale.ENGLISH);
        numberFormat.setMaximumFractionDigits(max);
        numberFormat.setGroupingUsed(false);
        return numberFormat;
    }
    public static String byte2Hex(byte data) {
        return String.format("%02X ", data);
    }
    public static float getCurrentTimeZone() {
        TimeZone tz = TimeZone.getDefault();
        String strTz = tz.getDisplayName(false, TimeZone.SHORT);
        Log.i(TAG, "getCurrentTimeZone: "+strTz);
        float _t = TimeZone.getDefault().getOffset(System.currentTimeMillis()) / (3600 * 1000f);

        Log.i(TAG, "getCurrentTimeZone: "+strTz);
        return _t;

    }
}
